﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Utility;

namespace YouHP.Pages.Ajax
{
    public class PatientsLikeMeActionList : YouHP.Pages.User.UserBasePage
    {
        protected int pageSize = 15;
        protected int pageIndex = 1;
        protected int dataCount = 0;
        protected int pageCount = 0;
        protected string PagingString;
        public List<UserActionInfo> ActionList;
        //提取出好友动态
        public List<UserActionInfo> ActionList2 = new List<UserActionInfo>();
        //要删除的好友动态
        public List<UserActionInfo> ActionList3 = new List<UserActionInfo>();

        protected override void ShowPage()
        {
            base.ShowPage();
            BuildXML();
        }


        protected void BuildXML()
        {

            PatientRecordInfo PrData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);
            ActionList = UserActionBLL.Instance.GetPatientsLikeMeActionList(UserData.Id,PrData.DiseaseId, 7);

            //提取出好友动态
            foreach (UserActionInfo item in ActionList)
            {
                if (item.ActionType == Constants.ACTION_FRIEND)
                {
                    ActionList2.Add(item);
                }
            }

            //提取出无效的好友动态
            foreach (UserActionInfo item in ActionList2)
            {

                int[] fidsarray = GetFidsArray(item.ActionContent);
                for (int i = 0; i < fidsarray.Length; i++)
                {
                    if (fidsarray[i] == UserData.Id)
                    {
                        item.ActionContent = item.ActionContent.Replace(fidsarray[i].ToString() + "@", "");
                        break;
                    }

                }
                if (item.ActionContent == "")
                {
                    ActionList3.Add(item);
                }

                RemoveItemsNotNeed(fidsarray, item.UserId);

            }

            //删除无效的好友动态
            foreach (UserActionInfo item in ActionList3)
            {
                ActionList.Remove(item);
            }


            dataCount = ActionList.Count;
            pageCount = Utils.GetPageCount(dataCount, pageSize);

            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            int num = 0;
            foreach (UserActionInfo item in ActionList)
            {

                string TempUrl = "/Profile/Index.aspx?uid=" + item.UserId;
                sb.Append("<tr><td width=\"60px\"><a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">" + item.UserName + "</a></td>");

                switch (item.ActionType)
                {
                    case Constants.ACTION_FRIEND:
                        int[] fidsarray = GetFidsArray(item.ActionContent);
                        item.ActionContent = "与&nbsp;";
                        for (int i = 0; i < fidsarray.Length; i++)
                        {
                            item.ActionContent += "<a href=\"" + this.GetUrl("/Profile/Index.aspx?uid=" + fidsarray[i]) + "\" target=\"_blank\">" + UserBLL.Instance.GetUserInfo(fidsarray[i]).UserName + "</a>&nbsp;";

                        }
                        item.ActionContent += "成为了好友";
                        break;
                    case Constants.ACTION_LOG:
                        item.ActionContent = "写了新日志" + item.ActionContent;
                        break;
                    case Constants.ACTION_SHARE:
                        item.ActionContent = item.ActionContent;
                        break;
                    case Constants.ACTION_TOPIC:
                        item.ActionContent = item.ActionContent;
                        break;
                    case Constants.ACTION_INDICATOR:
                        item.ActionContent = "记录了实验室指标" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_SYMPTOM:
                        item.ActionContent = "记录了症状" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_WEIGHT:
                        item.ActionContent = "记录了体重" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_ADD:
                        item.ActionContent = "开始使用药物" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_STOP:
                        item.ActionContent = "停止使用药物" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                    case Constants.ACTION_TREATMENT_CHANGE:
                        item.ActionContent = "改变了药物的使用剂量" + item.ActionContent + "&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\" target=\"_blank\">>>查看详情</a>";
                        break;
                }

                sb.Append("<td>" + item.ActionContent + "</td>");

                TempUrl = item.ActionTime.ToString("yyyy-MM-dd HH:mm");
                sb.Append("<td align=\"center\" width=\"120px\"><font color=#808080>" + TempUrl + "</font></td></tr>");

                num++;
                if (num % pageSize == 0)
                {
                    sb.Append("</table>");
                    pageIndex = num / pageSize;
                    PagingString = this.GetPagingString2(pageIndex, pageCount, "", "javascript:ViewPatientsLikeMeActionsPage");
                    sb.Append("<div class=\"pages\" style=\"padding:10px 5px;\">" + PagingString + "</div>");

                    sb.Append("@@@");
                    sb.Append("<table>");
                }
            }
            sb.Append("</table>");
            pageIndex = num / pageSize + 1;
            PagingString = this.GetPagingString2(pageIndex, pageCount, "", "javascript:ViewPatientsLikeMeActionsPage");
            sb.Append("<div class=\"pages\" style=\"padding:10px 5px;\">" + PagingString + "</div>");


            ResponseXML(sb);

        }

        protected int[] GetFidsArray(string str)
        {
            string[] array = str.Split('@');
            List<int> fids = new List<int>();
            for (int i = 0; i < array.Length - 1; i++)
            {
                if (array[i] != "")
                {
                    fids.Add(Convert.ToInt32(array[i]));
                }
            }

            int[] fidsarray = fids.ToArray();
            return fidsarray;
        }

        protected void RemoveItemsNotNeed(int[] fidsarray, int HostUserId)
        {

            for (int i = 0; i < fidsarray.Length; i++)
            {
                foreach (UserActionInfo item in ActionList2)
                {
                    if (item.UserId == fidsarray[i])
                    {
                        int[] fidsarray2 = GetFidsArray(item.ActionContent);
                        for (int j = 0; j < fidsarray2.Length; j++)
                        {
                            if (fidsarray2[j] == HostUserId)
                            {
                                item.ActionContent = item.ActionContent.Replace(fidsarray2[j].ToString() + "@", "");
                                break;
                            }

                        }
                        if (item.ActionContent == "")
                        {
                            ActionList3.Add(item);
                        }
                        break;

                    }
                }
            }
        }


        protected void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}